﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ETL.Entity;
namespace ETL.DataAccess
{
    public class ProjectTaskRepository
    {
        public IEnumerable<TASK> GetProjectTasks(int? projectId)
        {
            if (projectId.HasValue)
            {
                var db = new ETLDataEntities();
                var result = from s in db.TASKS 
                             where s.GEN_PROJECT_ID == projectId  
                             orderby s.GEN_TASK_ID
                             select s;
                return result;
            }
            else return null;
        }

        public TASK GetProjectTask(int? projectId, int? taskId)
        {
            TASK result = null;
            if (taskId.HasValue)
            {
                var db = new ETLDataEntities();
                var allTasks = from s in db.TASKS
                             where s.GEN_TASK_ID == taskId && s.GEN_PROJECT_ID == projectId
                             select s;
                if (allTasks.Count() > 0)
                {
                    return allTasks.First();
                }
            }
            return result;
        }

        /**public void Update(project_tasks task)
        {
            var db = new ETLDataEntities();
            project_tasks taskToUpdate = (from s in db.project_tasks where s.t_task == task.t_task select s).First();

            taskToUpdate.t_active = task.t_active;
            taskToUpdate.t_bud_comp = task.t_bud_comp;
            taskToUpdate.t_category = task.t_category;
            taskToUpdate.t_description = task.t_description;
            taskToUpdate.t_end_date = task.t_end_date;
            taskToUpdate.t_est_comp = task.t_est_comp;
            taskToUpdate.t_est_hours = task.t_est_hours;
            taskToUpdate.t_est_modified = task.t_est_modified;
            taskToUpdate.t_first_completion = task.t_first_completion;
            taskToUpdate.t_heading = task.t_heading;
            taskToUpdate.t_hours_remaining = task.t_hours_remaining;
            taskToUpdate.t_level = task.t_level;
            taskToUpdate.t_line = task.t_line;
            taskToUpdate.t_notes = task.t_notes;
            taskToUpdate.t_original_hours = task.t_original_hours;
            taskToUpdate.t_predecessor = task.t_predecessor;
            taskToUpdate.t_project = task.t_project;
            taskToUpdate.t_resource = task.t_resource;
            taskToUpdate.t_start_date = task.t_start_date;
            taskToUpdate.t_started = task.t_started;
            taskToUpdate.t_status = task.t_status;
            taskToUpdate.t_task = task.t_task;
            taskToUpdate.t_wbs = task.t_wbs;

            db.Entry(taskToUpdate).State = EntityState.Modified;

            db.SaveChanges();

        }**/

        public TASK GetLastProjectTask(string userid)
        {
            var db = new ETLDataEntities();
            //var sql = "SELECT projects.pr_name, project_times.time_project, project_times.time_category, project_times.time_task_id, " +
            //         "project_tasks.t_description, project_categories.c_description " +
            //         "FROM project_categories " +
            //         "INNER JOIN project_times ON project_categories.c_category = project_times.time_category " +
            //         "AND project_categories.c_project = project_times.time_project " +
            //         "INNER JOIN project_tasks ON project_times.time_task_id = project_tasks.t_task " +
            //         "AND project_times.time_project = project_tasks.t_project " +
            //         "INNER JOIN projects ON project_times.time_project = projects.pr_ref " +
            //         "WHERE (project_times.time_project > 0) AND (project_times.time_user_id = '" + userid + "') " +
            //         "ORDER BY project_times.time_start DESC";

            var lastestProjectTime = ( from t in db.TASKS
                                       join a in db.ETL_ACTUAL_RESOURCE_USAGES on t.GEN_TASK_ID equals a.GEN_TASK_ID
                                       join r in db.RESOURCE_INSTANCES on a.GEN_RESOURCE_ID equals r.GEN_RESOURCE_ID
                                       where r.RESOURCE_NAME == userid 
                                       orderby a.GEN_ACTUAL_RESOURCE_USAGE_ID descending 
                                       select t).FirstOrDefault();

            return lastestProjectTime;
           
        }

        public void GetTasksOfThisWeek(int? projectId, int? categoryId)
        {

        }
    }
}
